//
//  UIImageView+corner.m
//  sada
//
//  Created by 朱国良 on 2017/12/26.
//  Copyright © 2017年 朱国良. All rights reserved.
//

#import "UIImageView+corner.h"
#import "SDImageCache.h"
#import "UIImageView+WebCache.h"
#import <YYCategories/YYCategories.h>

@implementation UIImageView (corner)

- (void)xf_loadImageUrlStr:(NSString *)urlStr placeHolderImageName:(UIImage *)placeHolder radius:(CGFloat)radius{
    //头像需要手动缓存处理成圆角的图片
    NSString *cacheurlStr = [urlStr stringByAppendingString:@"radiusCache"];
    UIImage *cacheImage = [[SDImageCache sharedImageCache] imageFromCacheForKey:cacheurlStr];
    if (cacheImage) {
        self.image = cacheImage;
        return ;
    }
    
    NSURL *url = [[NSURL alloc] initWithString:urlStr];
    if (radius != 0.0) {
        [self sd_setImageWithURL:url placeholderImage:placeHolder completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (!error) {
                UIImage *radiusImage = [image imageByRoundCornerRadius:radius];
                self.image = radiusImage;
                [[SDImageCache sharedImageCache] storeImage:radiusImage forKey:cacheurlStr completion:nil];
                //清除原有非圆角图片缓存
                [[SDImageCache sharedImageCache] removeImageForKey:urlStr withCompletion:nil];
            }
        }];
    }else {
        [self sd_setImageWithURL:url placeholderImage:placeHolder options:SDWebImageProgressiveDownload];
    }
}
@end

